// Copyright (c) 2023, szufisher and contributors
// For license information, please see license.txt

frappe.ui.form.on('Warehouse Task', {
	setup(frm){
		frm.set_query("storage_bin", "items", function(doc, cdt, cdn) {
			const child = locals[cdt][cdn];
			let storage_bin_used = frm.doc.items
			.filter(r => {return (r.name !== child.name && r.storage_bin)})
			.map(r=>{return r.storage_bin});
			return {
				query: "zelin_wms.storage_bin_management.doctype.warehouse_task.warehouse_task.get_storage_bin",
				filters: {
					'disabled': 0,
					'warehouse': child.warehouse,
					'storage_bin_used': storage_bin_used,
					'item_code': child.item_code,
					'batch_no': child.batch_no,
					'stock_qty': child.stock_qty || 0,
					'warehouse_task_type': frm.doc.warehouse_task_type
				}
			}			
		});
	}
	// refresh: function(frm) {

	// }
});

frappe.ui.form.on('Warehouse Task Detail', {
	qty(frm, cdt, cdn){
		const child = locals[cdt][cdn];
		frappe.model.set_value(cdt, cdn, 'stock_qty', child.qty * child.conversion_rate || 1)
	}
})